<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跳跃表 vs 红黑树：Redis Sorted Set 的设计选择</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .section-divider {
            position: relative;
            text-align: center;
            margin: 3rem 0;
        }
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
        }
        .section-divider span {
            background: white;
            padding: 0 2rem;
            position: relative;
            color: #9ca3af;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-purple-600 via-indigo-600 to-blue-600 text-white">
        <div class="absolute inset-0 bg-black opacity-10"></div>
        <div class="relative container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                    跳跃表 vs 红黑树
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    深入解析 Redis Sorted Set 的设计选择
                </p>
                <div class="flex justify-center space-x-4 text-lg">
                    <span class="flex items-center">
                        <i class="fas fa-layer-group mr-2"></i>
                        数据结构
                    </span>
                    <span class="flex items-center">
                        <i class="fas fa-tachometer-alt mr-2"></i>
                        性能优化
                    </span>
                    <span class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        系统设计
                    </span>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 105C120 90 240 60 360 45C480 30 600 30 720 37.5C840 45 960 60 1080 67.5C1200 75 1320 75 1380 75L1440 75V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0Z" fill="#F9FAFB"/>
            </svg>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8 md:p-12 card-hover">
                <p class="text-lg leading-relaxed text-gray-700 drop-cap">
                    Sorted Set 使用跳跃表而非红黑树主要是因为跳跃表在实现简单性、空间效率和动态操作性能方面提供了显著的优势。尽管红黑树在理论上也具有良好的性能，但跳跃表的实际应用中提供了更优的性能和更易于维护的实现，尤其适合像 Redis 这样的高性能需求场景。
                </p>
            </div>
        </section>

        <!-- 跳跃表的优势 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <span class="gradient-text mr-3">1.</span>
                跳跃表的优势
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <!-- 实现简单 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="feature-icon bg-gradient-to-br from-blue-500 to-blue-600 text-white">
                        <i class="fas fa-code"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">1.1 实现简单</h3>
                    <p class="text-gray-600 leading-relaxed">
                        <strong class="text-gray-800">跳跃表</strong>是一种基于链表的数据结构，通过多层链表来实现有序数据的快速访问。其实现相对简单，尤其是对于动态更新（如插入、删除）操作。
                    </p>
                    <div class="mt-4 p-4 bg-gray-50 rounded-lg">
                        <p class="text-sm text-gray-600">
                            <strong class="text-gray-700">红黑树</strong>是一种自平衡的二叉搜索树，其实现更复杂，尤其是在插入、删除操作时需要维持树的平衡。
                        </p>
                    </div>
                </div>

                <!-- 查找、插入和删除效率 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="feature-icon bg-gradient-to-br from-green-500 to-green-600 text-white">
                        <i class="fas fa-rocket"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">1.2 操作效率</h3>
                    <p class="text-gray-600 leading-relaxed">
                        <strong class="text-gray-800">跳跃表</strong>：具有 <code class="bg-gray-100 px-2 py-1 rounded text-sm">O(log n)</code> 时间复杂度的查找、插入和删除操作。其多级链表允许跳跃访问，提供了高效的性能。
                    </p>
                    <div class="mt-4 p-4 bg-gray-50 rounded-lg">
                        <p class="text-sm text-gray-600">
                            <strong class="text-gray-700">红黑树</strong>：同样具有 <code class="bg-gray-100 px-2 py-1 rounded text-xs">O(log n)</code> 的时间复杂度，但由于需要在每次操作后维护平衡，操作的复杂度相对较高。
                        </p>
                    </div>
                </div>

                <!-- 空间效率 -->
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <div class="feature-icon bg-gradient-to-br from-purple-500 to-purple-600 text-white">
                        <i class="fas fa-database"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">1.3 空间效率</h3>
                    <p class="text-gray-600 leading-relaxed">
                        <strong class="text-gray-800">跳跃表</strong>的空间复杂度是 <code class="bg-gray-100 px-2 py-1 rounded text-sm">O(n log n)</code>，其中的额外空间用于维护多层索引链表。实际内存开销较小，且空间分配是动态的。
                    </p>
                    <div class="mt-4 p-4 bg-gray-50 rounded-lg">
                        <p class="text-sm text-gray-600">
                            <strong class="text-gray-700">红黑树</strong>的空间复杂度是 <code class="bg-gray-100 px-2 py-1 rounded text-xs">O(n)</code>，但每个节点都需要额外存储平衡信息（如颜色和子树高度），导致内存开销相对较大。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- 数据结构对比可视化 -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-indigo-50 to-purple-50 rounded-2xl p-8">
                <h3 class="text-2xl font-semibold mb-6 text-center text-gray-800">数据结构对比分析</h3>
                <div class="mermaid">
                    graph TD
                        A[数据结构选择] --> B[跳跃表]
                        A --> C[红黑树]
                        
                        B --> D[实现简单]
                        B --> E[动态操作高效]
                        B --> F[空间动态分配]
                        B --> G[常数因子小]
                        
                        C --> H[实现复杂]
                        C --> I[需要平衡操作]
                        C --> J[固定空间开销]
                        C --> K[理论性能优秀]
                        
                        D --> L[适合Redis]
                        E --> L
                        F --> L
                        G --> L
                        
                        style A fill:#667eea,stroke:#5a67d8,color:#fff
                        style B fill:#48bb78,stroke:#38a169,color:#fff
                        style C fill:#ed8936,stroke:#dd6b20,color:#fff
                        style L fill:#9f7aea,stroke:#805ad5,color:#fff
                </div>
            </div>
        </section>

        <!-- 跳跃表 vs. 红黑树 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <span class="gradient-text mr-3">2.</span>
                跳跃表 vs. 红黑树
            </h2>

            <div class="grid md:grid-cols-2 gap-8">
                <!-- 实现复杂性 -->
                <div class="bg-white rounded-xl shadow-lg overflow-hidden">
                    <div class="bg-gradient-to-r from-blue-500 to-indigo-600 p-6">
                        <h3 class="text-xl font-semibold text-white flex items-center">
                            <i class="fas fa-cogs mr-3"></i>
                            2.1 实现复杂性
                        </h3>
                    </div>
                    <div class="p-6">
                        <div class="space-y-4">
                            <div class="flex items-start">
                                <div class="flex-shrink-0 w-10 h-10 bg-green-100 rounded-full flex items-center justify-center mr-4">
                                    <i class="fas fa-check text-green-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-semibold text-gray-800 mb-1">跳跃表</h4>
                                    <p class="text-gray-600">实现相对简单，尤其是在动态操作时不需要复杂的树旋转或重平衡操作。</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="flex-shrink-0 w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center mr-4">
                                    <i class="fas fa-exclamation text-orange-600"></i>
                                </div>
                                <div>
                                    <h4 class="font-semibold text-gray-800 mb-1">红黑树</h4>
                                    <p class="text-gray-600">实现复杂，涉及树节点的旋转和重新着色，代码难度较大。</p>